package com.qf.studentmanager.service;

import com.qf.studentmanager.dao.ClazzDao;
import com.qf.studentmanager.model.Clazz;
import com.qf.studentmanager.model.RespPageBean;

import java.sql.SQLException;
import java.util.List;

public class ClazzService {
    ClazzDao clazzDao = new ClazzDao();

    public RespPageBean getAllClazzByPage(String page, String rows, String sort, String oder, String gid) {
        if (page == null){
            page = "1";
        }
        if (rows == null){
            rows = "10";
        }
        if (sort == null){
            sort = "cid";
        }
        if (oder == null){
            oder ="asc";
        }
        RespPageBean respPageBean = new RespPageBean();
        Long total = clazzDao.getTotal(gid);//根据年纪查，

        int p = Integer.parseInt(page);
        int r = Integer.parseInt(rows);

        List<Clazz> list = clazzDao.getClazzByPage((p-1)* r,r,sort,oder,gid);
        respPageBean.setTotal(total);
        respPageBean.setRows(list);
        return respPageBean;
    }


    public Integer addClazz(Clazz clazz) {
        Clazz c = clazzDao.getClazzByName(clazz.getClazzName());

        if (c != null){
            return -2;
        }else {
            try {
                return  clazzDao.addClazz(clazz);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }


    public List<Clazz> getClazzByGid(String gid) {
        return clazzDao.getClazzByGid(gid);
    }

    public Integer deteteClazzByCid(String cid) {
        return clazzDao.deleteClazzByCid(cid);
    }
}
